package com.cheyunkeji.er.activity.evaluate;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;
import com.cheyunkeji.er.http.ApiClient;
import com.cheyunkeji.er.http.HttpConstants;
import com.cheyunkeji.er.http.RespCallback;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;

public class CarFrameworkBCSM extends BaseActivity implements CompoundButton.OnCheckedChangeListener {


    private static final String TAG = CarFrameworkBCSM.class.getSimpleName();
    @BindView(R.id.v_topbar)
    TopBar vTopbar;
    @BindView(R.id.cb_lite_00)
    CheckBox cbLite00;
    @BindView(R.id.cb_heavy_00)
    CheckBox cbHeavy00;
    @BindView(R.id.cb_fix_00)
    CheckBox cbFix00;
    @BindView(R.id.ll_group0)
    LinearLayout llGroup0;
    @BindView(R.id.cb_lite_01)
    CheckBox cbLite01;
    @BindView(R.id.cb_heavy_01)
    CheckBox cbHeavy01;
    @BindView(R.id.cb_fix_01)
    CheckBox cbFix01;
    @BindView(R.id.ll_group1)
    LinearLayout llGroup1;
    @BindView(R.id.cb_lite_02)
    CheckBox cbLite02;
    @BindView(R.id.cb_heavy_02)
    CheckBox cbHeavy02;
    @BindView(R.id.cb_fix_02)
    CheckBox cbFix02;
    @BindView(R.id.ll_group2)
    LinearLayout llGroup2;
    @BindView(R.id.cb_lite_03)
    CheckBox cbLite03;
    @BindView(R.id.cb_heavy_03)
    CheckBox cbHeavy03;
    @BindView(R.id.cb_fix_03)
    CheckBox cbFix03;
    @BindView(R.id.ll_group3)
    LinearLayout llGroup3;
    @BindView(R.id.cb_lite_04)
    CheckBox cbLite04;
    @BindView(R.id.cb_heavy_04)
    CheckBox cbHeavy04;
    @BindView(R.id.cb_fix_04)
    CheckBox cbFix04;
    @BindView(R.id.ll_group4)
    LinearLayout llGroup4;
    @BindView(R.id.cb_lite_05)
    CheckBox cbLite05;
    @BindView(R.id.cb_heavy_05)
    CheckBox cbHeavy05;
    @BindView(R.id.cb_fix_05)
    CheckBox cbFix05;
    @BindView(R.id.ll_group5)
    LinearLayout llGroup5;
    @BindView(R.id.cb_lite_06)
    CheckBox cbLite06;
    @BindView(R.id.cb_heavy_06)
    CheckBox cbHeavy06;
    @BindView(R.id.cb_fix_06)
    CheckBox cbFix06;
    @BindView(R.id.ll_group6)
    LinearLayout llGroup6;
    @BindView(R.id.cb_lite_07)
    CheckBox cbLite07;
    @BindView(R.id.cb_heavy_07)
    CheckBox cbHeavy07;
    @BindView(R.id.cb_fix_07)
    CheckBox cbFix07;
    @BindView(R.id.ll_group7)
    LinearLayout llGroup7;
    @BindView(R.id.cb_lite_08)
    CheckBox cbLite08;
    @BindView(R.id.cb_heavy_08)
    CheckBox cbHeavy08;
    @BindView(R.id.cb_fix_08)
    CheckBox cbFix08;
    @BindView(R.id.ll_group8)
    LinearLayout llGroup8;
    @BindView(R.id.cb_lite_09)
    CheckBox cbLite09;
    @BindView(R.id.cb_heavy_09)
    CheckBox cbHeavy09;
    @BindView(R.id.cb_fix_09)
    CheckBox cbFix09;
    @BindView(R.id.ll_group9)
    LinearLayout llGroup9;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.activity_car_framework_bcsm)

    LinearLayout activityCarFrameworkBcsm;

    private ArrayList<LinearLayout> llList;


    @Override
    protected void init() {

        setContentView(R.layout.er_activity_car_framework_bcsm);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {

        vTopbar.setLeftBack();
        vTopbar.setTitle("车身骨架补充说明");
        vTopbar.setRight("保存", -1, this);
        tvSave.setOnClickListener(this);

        cbFix00.setOnCheckedChangeListener(this);
        cbLite00.setOnCheckedChangeListener(this);
        cbHeavy00.setOnCheckedChangeListener(this);
        cbFix01.setOnCheckedChangeListener(this);
        cbLite01.setOnCheckedChangeListener(this);
        cbHeavy01.setOnCheckedChangeListener(this);
        cbFix02.setOnCheckedChangeListener(this);
        cbLite02.setOnCheckedChangeListener(this);
        cbHeavy02.setOnCheckedChangeListener(this);
        cbFix03.setOnCheckedChangeListener(this);
        cbLite03.setOnCheckedChangeListener(this);
        cbHeavy03.setOnCheckedChangeListener(this);
        cbFix04.setOnCheckedChangeListener(this);
        cbLite04.setOnCheckedChangeListener(this);
        cbHeavy04.setOnCheckedChangeListener(this);
        cbFix05.setOnCheckedChangeListener(this);
        cbLite05.setOnCheckedChangeListener(this);
        cbHeavy05.setOnCheckedChangeListener(this);
        cbFix06.setOnCheckedChangeListener(this);
        cbLite06.setOnCheckedChangeListener(this);
        cbHeavy06.setOnCheckedChangeListener(this);
        cbFix07.setOnCheckedChangeListener(this);
        cbLite07.setOnCheckedChangeListener(this);
        cbHeavy07.setOnCheckedChangeListener(this);
        cbFix08.setOnCheckedChangeListener(this);
        cbLite08.setOnCheckedChangeListener(this);
        cbHeavy08.setOnCheckedChangeListener(this);
        cbFix09.setOnCheckedChangeListener(this);
        cbLite09.setOnCheckedChangeListener(this);
        cbHeavy09.setOnCheckedChangeListener(this);

        tvSave.setOnClickListener(this);

    }

    @Override
    protected void initData() {


    }

    @Override
    protected void onResume() {
        super.onResume();

        if (null == llList) {
            llList = new ArrayList<>();
            llList.add(llGroup0);
            llList.add(llGroup1);
            llList.add(llGroup2);
            llList.add(llGroup3);
            llList.add(llGroup4);
            llList.add(llGroup5);
            llList.add(llGroup6);
            llList.add(llGroup7);
            llList.add(llGroup8);
            llList.add(llGroup9);
        }

        if (MyApplication.getInstance().getEvaluateResult() != null)
            displayInfo();

    }

    private void displayInfo() {
        EvaluateDetailResult2.DetectionBean detection = MyApplication.getInstance().getEvaluateResult().getDetection();
        if (null != detection && null != detection.getBodyframe_mark()) {
            for (int i = 0; i < detection.getBodyframe_mark().size(); i++) {
                ((CheckBox) llList.get(detection.getBodyframe_mark().get(i).getId()).getChildAt(detection.getBodyframe_mark().get(i).getStatus() - 1)).setChecked(true);
                llList.get(detection.getBodyframe_mark().get(i).getId()).setTag(detection.getBodyframe_mark().get(i).getStatus());
            }
        }
    }

//   LinearLayout 设置的Tag 为该项的状态值 ： 1-轻微 2-严重 3-需换修

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        LinearLayout container = (LinearLayout) buttonView.getParent();
        if (isChecked) {
            for (int i = 0; i < container.getChildCount(); i++) {
                if (buttonView != container.getChildAt(i) && ((CheckBox) container.getChildAt(i)).isChecked()) {
                    ((CheckBox) container.getChildAt(i)).setChecked(false);
                } else if (buttonView == container.getChildAt(i)) {
                    container.setTag(i + 1);
                }
            }
            Log.e(TAG, "onCheckedChanged:  true :  " + ((LinearLayout) buttonView.getParent()).getTag());
        } else {
            if (buttonView == container.getChildAt((int) container.getTag() - 1)) {
                container.setTag(null);
            }
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        switch (v.getId()) {

            case R.id.tv_save:
            case R.id.vRight:
                saveResult();
                break;

            default:
                break;
        }
    }


    /**
     * 保存结果
     */
    private void saveResult() {
        List<EvaluateDetailResult2.DetectionBean.BodyframeMarkBean> beanList = new ArrayList<>();
        Gson gson = new Gson();
        for (int i = 0; i < llList.size(); i++) {
            if (null != llList.get(i).getTag()) {
                beanList.add(new EvaluateDetailResult2.DetectionBean.BodyframeMarkBean(i, (int) llList.get(i).getTag()));
            }
        }

        String jsonStr = new StringBuilder("{\"bodyframe_mark\":").append(gson.toJson(beanList)).append("}").toString();

        Log.e(TAG, "saveResult: " + jsonStr);

        HashMap<String, String> params = new HashMap<>();
//        params.put("authkey", UserInfoManager.getInstance(MyApplication.getInstance()).getAuthKey());
//        params.put(ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getYkey(), ChannelInfoUtil.getCurrentChannelInfo().getCkey1().getY7e_e73a691f02a46500201c3998e6a2989d());
        params.put("aid", MyApplication.getInstance().getEvaluateResult().getAid());
        params.put("detection", jsonStr);

        ApiClient.postForm(HttpConstants.INSERT_ARCHIVE_INFO, params, new RespCallback<EvaluateDetailResult2>() {

            @Override
            protected void onRequestFailed(String msg) {
                SToast.show(msg);
            }

            @Override
            protected void onSuccess(EvaluateDetailResult2 data) {
                if (null != data) {
                    MyApplication.getInstance().setEvaluateResult(data);
                    SToast.show("保存成功");
                    MyApplication.getInstance().removeActivity(CarFrameworkBCSM.class);
                    finish();
                }
            }

            @Override
            public void onBefore(Request request, int id) {
                showDialog();
            }

            @Override
            public void onAfter(int id) {
                dismissDialog();
            }

        });
    }

}
